<?php

/**
 * 六象元一 赠品函数库
 * ============================================================================
 * $Author: 王海龙 $
 * $Id: lib_gift.php 17217 2014-11-04 00:00:00Z 王海龙 $
*/

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

function wait($source = 'pc'){
    if(time() < 1416131700){
        return false;
    }
    require(ROOT_PATH . 'includes/cls_cachelock.php');
    if($_SERVER['SCRIPT_NAME'] == '/respond.php' OR $_SERVER['SCRIPT_NAME'] == '/mobile/pay_return_y.php' OR $_SERVER['SCRIPT_NAME'] == '/mobile/pay_return.php'){
        return false;
    }
    $lock = new CacheLock('lock_wait','/lock_wait');
    $lock->lock();
    $time = time();//当前时间
    $memcached_key = 'eggms_wait_list';
    $uv_key = 'eggms_uv';
    $link_count  = 450;//最大连接数
    $out_time = 600;//脱离队列时间
    $out_time2 = 60;// 失败重排列时间
    $failure_up_time  = 600;//成功cookie更新时间 
    $failure_die_time = time()+3600;//失败cookie消失时间
    $success_die_time = time()+3600;//成功cookie消失时间
    $cookieid = $_COOKIE['YC']['cookieid'];
    if(!$cookieid){
        $cookieid = md5(md5(time()).rand(10000,99999).rand(10000,99999));
        setcookie("YC[cookieid]",$cookieid, $failure_die_time);
    }else{
        $wait_success = empty($_COOKIE['YC']['wait_success']) ? '' : $_COOKIE['YC']['wait_success'];//用户是否已经成功进入
    }
    // if(empty($wait_success)){
    //     header("location: http://wait.eggms.com?".rand());
    //     exit();
    // }

    if(empty($wait_success) && $_COOKIE['YC']['wait_uptime'] + $out_time2 > time()){
        $lock->unlock();
    }else{
        // 连接memcached缓存
        $memcached = new memcached;
        $memcached->addServer('localhost', 11211) or die("连接失败"); 
        $wait_list = $memcached->get($memcached_key);
        $uv_list = $memcached->get($uv_key);
        $uv_list[$cookieid] = time();
        $memcached->set($uv_key,$uv_list, time()+3600);
        foreach ($wait_list as $key => $value) {
            if($value <= $time - $out_time) unset($wait_list[$key]);
        }
        if(!$wait_success){
            if($_COOKIE['YC']['wait_uptime'] + $out_time2 <= time()){
                $count = count($wait_list);
                if($count < $link_count){
                    $wait_list[$cookieid] = $time;
                    $wait_success = 1;
                    setcookie("YC[wait_success]",1, $success_die_time);
                    setcookie("YC[wait_uptime]",time(), $success_die_time);
                }else{
                    setcookie("YC[wait_uptime]",time(), $failure_die_time);
                }
            }
        }else{
            if($_COOKIE['YC']['wait_uptime']+$out_time >= time()){
                $wait_list[$cookieid] = $time;
                $wait_success = 1;
                setcookie("YC[wait_success]",1, $success_die_time);
                setcookie("YC[wait_uptime]",time(), $success_die_time);
            }else{
                $count = count($wait_list);
                if($count < $link_count){
                    $wait_list[$cookieid] = $time;
                    $wait_success = 1;
                    setcookie("YC[wait_success]",1, $success_die_time);
                    setcookie("YC[wait_uptime]",time(), $success_die_time);
                }else{
                    setcookie("YC[wait_success]",0, $success_die_time);
                    setcookie("YC[wait_uptime]",time(), $failure_die_time);
                }
            }
        }
        $memcached->set($memcached_key,$wait_list, time()+3600);
        $lock->unlock();
    }
    //如果未进入列队显示正在排队
    if(empty($wait_success)){
        header("location: http://wait.eggms.com?".rand());
        exit();
    }
}
?>
